package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo11 {
    //完成登录,并且登录不成功时,需要知道是密码错误还是用户名不存在
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();
        try(Connection conn = DBUtils.getConn()) {
            String sql = "select password from user where username = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            //1代表是第一个问号
            ps.setString(1,username);
            //执行查询
            ResultSet rs = ps.executeQuery();
            //判断是否有数据
            if (rs.next()) {
                //1代表是查询出的第一个字段
                String pw = rs.getString(1);
                if (pw.equals(password)) {
                    System.out.println("登录成功!");
                } else {
                    System.out.println("密码错误!");
                }
            } else {
                System.out.println("用户名不存在!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
